<?php

namespace app\admin\controller\mall;

use app\api\model\mysql\MallAccountLog;
use app\api\model\mysql\MallUser;
use app\api\model\mysql\MallWithdrawal;
use app\common\controller\AdminController;
use app\common\lib\ArrClass;
use think\App;

class Withdrawal extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallWithdrawal();
    }

    public function index($id=''){
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order('status ASC,create_time desc')
                ->select();
            foreach ($list as $k=>$v){
                $minfo =(new MallUser())->getUserByList($v['openid']);
                $list[$k]['nickName'] =$minfo['nickName'];
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 处理提现
     * @param $id
     * @return void
     */
    function tixian($id=''){
        $row=$this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if($row['status'] ==1){
                $this->error('已审核通过，无需重复操作');
            }
            // 同意打款且自动打款
            if($post['dotype']==1 && $post['status']==1){
                $objClass = ArrClass::payAttr();
                $obj = ArrClass::initClass('wx', $objClass, [], true);
                // 提现接口
                $notify= $obj->transfer([
                    'partner_trade_no' => $row['ordersn'], // 商户订单号，需保持唯一性(只能是字母或者数字，不能包含有符号)
                    'openid' => $row['openid'],
                    'check_name' => 'FORCE_CHECK', // NO_CHECK：不校验真实姓名, FORCE_CHECK：强校验真实姓名
                    're_user_name' => $row['realname'], // 如果 check_name 设置为FORCE_CHECK，则必填用户真实姓名
                    'amount' => $row['money']*100, // 企业付款金额，单位为分
                    'desc' => '共享商城提现', // 企业付款操作说明信息。必填
                ]);

                if($notify && $notify['return_code'] == 'SUCCESS'){
                    if($notify['result_code'] == 'SUCCESS'){
                        $this->model->where("ordersn",$row['ordersn'])->save(array('status'=>1,'dotype'=>1));
                        (new MallAccountLog())->saveAccoutLog(0,$row['openid'],$row['money'],2,'会员提现');
                    }else{
                        $this->error($notify['err_code_des']);
                    }
                }
                else{
                    $this->error("审核失败");
                }
            }else{
                $row->save($post);
                // 拒绝打款，退回申请打款金额
                if($post['status'] ==2){
                    switch($row['type']){
                        case 3:
                            (new MallUser())->where("openid",$row['openid'])->inc('balance',$row['money'])->update();
                            break;
                        case 4:
                            (new MallUser())->where("openid",$row['openid'])->inc('againbalance',$row['money'])->update();
                            break;
                        case 5:
                            (new MallUser())->where("openid",$row['openid'])->inc('teambalance',$row['money'])->update();
                            break;
                        default:
                            break;
                    }

                }
            }

            $this->success('保存成功');
        }
        $this->assign('row',$row);
        return $this->fetch();
    }
}